Completed
Pull Request — develop (#161)
by Wachter
14:20
created

main.js ➔ define   B

Complexity

Conditions 1
Paths 384

Size

Total Lines 125

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
c 2
b 0
f 1
nc 384
nop 1
dl 0
loc 125
rs 7.1707

11 Functions

Rating   Name   Duplication   Size   Complexity  
A main.js ➔ ... ➔ generatePath 0 3 1
A main.js ➔ ... ➔ trimSlash 0 3 1
A main.js ➔ ... ➔ .setSuffix 0 11 2
A main.js ➔ ... ➔ .render 0 9 1
A main.js ➔ ... ➔ .pageSelectClicked 0 20 1
A main.js ➔ ... ➔ .getData 0 3 1
A main.js ➔ ... ➔ .bindCustomEvents 0 2 1
A main.js ➔ ... ➔ .setData 0 3 1
A main.js ➔ ... ➔ .bindDomEvents 0 11 1
A main.js ➔ ... ➔ .setParentPage 0 13 4
A main.js ➔ ... ➔ .initialize 0 14 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/*
2
 * This file is part of the Sulu CMS.
3
 *
4
 * (c) MASSIVE ART WebServices GmbH
5
 *
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
define(['text!./skeleton.html'], function(skeletonTemplate) {
11
12
    'use strict';
13
14
    var defaults = {
15
            options: {
16
                historyApi: null,
17
                historyResultKey: 'routes',
18
                historyPathKey: 'path'
19
            },
20
            templates: {
21
                skeleton: skeletonTemplate
22
            },
23
            translations: {
24
                showHistory: 'public.show-history'
25
            }
26
        },
27
28
        trimSlash = function(string) {
29
            return string.replace(/\/+$/g, '');
30
        },
31
32
        generatePath = function(pagePath, suffix) {
33
            return trimSlash(pagePath) + '/' + suffix;
34
        };
35
36
    return {
37
        defaults: defaults,
38
39
        initialize: function() {
40
            // FIXME this adds the required class to the label
41
            this.$el.closest('.form-group').find('label').addClass('required');
42
43
            this.bindCustomEvents();
44
45
            this.render();
46
47
            var data = this.getData(),
48
                page = data.page || {};
49
50
            this.$prefix.val(page.path || '');
51
            this.$suffix.val(data.suffix || '');
52
        },
53
54
        bindCustomEvents: function() {
55
        },
56
57
        render: function() {
58
            this.html(this.templates.skeleton({translations: this.translations, options: this.options}));
59
60
            this.$prefix = this.$el.find('.prefix');
61
            this.$suffix = this.$el.find('.suffix');
62
            this.$choose = this.$el.find('.choose');
63
64
            this.bindDomEvents();
65
        },
66
67
        bindDomEvents: function() {
68
            this.$suffix.on('change', function(event) {
69
                this.setSuffix($(event.currentTarget).val());
70
            }.bind(this));
71
72
            this.$choose.on('click', function() {
73
                this.pageSelectClicked();
74
75
                return false;
76
            }.bind(this));
77
        },
78
79
        pageSelectClicked: function() {
80
            var $container = $('<div/>'),
81
                data = this.getData();
82
83
            this.$el.append($container);
84
85
            this.sandbox.start(
86
                [
87
                    {
88
                        name: 'page-tree-route/page-select@suluarticle',
89
                        options: {
90
                            el: $container,
91
                            selected: (data.page || {}).uuid || null,
92
                            locale: this.options.locale,
93
                            selectCallback: this.setParentPage.bind(this)
94
                        }
95
                    }
96
                ]
97
            );
98
        },
99
100
        setParentPage: function(item) {
101
            var data = this.getData();
102
            data.page = {uuid: item.id, path: (item.url || '/')};
103
104
            data.path = null;
105
            if (!!data.suffix && data.suffix.length > 0) {
106
                data.path = generatePath(data.page.path, data.suffix);
107
            }
108
109
            this.setData(data);
110
111
            this.$prefix.val(data.page.path === '/' ? data.page.path : trimSlash(data.page.path)).trigger('change');
112
        },
113
114
        setSuffix: function(suffix) {
115
            var data = this.getData();
116
            data.suffix = suffix;
117
118
            data.path = null;
119
            if (!!data.page) {
120
                data.path = generatePath(data.page.path, data.suffix);
121
            }
122
123
            this.setData(data);
124
        },
125
126
        setData: function(data) {
127
            this.$el.data('value', data);
128
        },
129
130
        getData: function() {
131
            return this.$el.data('value') || {};
132
        }
133
    };
134
});
135